package com.Modular.MinioPlugin;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.minio.BucketExistsArgs;
import io.minio.DownloadObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.UploadObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Bucket;

/**miniIo常用方法
 * @author Administrator
 *
 */
public class MinIoOperations {

	

	public static MinioClient minioClient = null ;
	
	/** 启动时候注册一下
	 * @param endpoint http://10.0.105.246:49000 运行端口49000是api的端口和49001是网页版的端口
	 * @param accessKey 账号
	 * @param secretKey 密码
	 * 链接错误的时候，可能是端口问题或者http没带导致的
	 */
	public static void addMainIo(String endpoint ,String accessKey, String secretKey )  {
		minioClient = MinioClient.builder()
		             .endpoint(endpoint)
		             .credentials(accessKey, secretKey)
		             .build();
	}
	
	
	
	
	
	
	
	/**
	 * 上传文件
	 * @param bucket 桶名
	 * @param name 上传后显示的文件名称
	 * @param filename 文件路径
	 */
	public static void fileUpload(String bucket ,String name ,String filename){
		bucketIf(bucket);
		try {
			minioClient.uploadObject(
			          UploadObjectArgs.builder()
			              .bucket(bucket)
			              .object(name)
			              .filename(filename)
			              .build());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取临时路径（put方式请求才能显示）
	 * @param bucket 桶名
	 * @param name 上传后显示的文件名称
	 * @return 2小时有效期的路径
	 * @throws Exception
	 */
	public static String fileGeturlPut(String bucket ,String name) {
		String url = null;
		try {
			url = minioClient.getPresignedObjectUrl(
				        GetPresignedObjectUrlArgs.builder()
				            .method(Method.PUT)
				            .bucket(bucket)
				            .object(name)
				            .expiry(2, TimeUnit.HOURS)//2小时有效
				            .build());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url ;
	}
	/**
	 * 获取临时路径（get方式请求才能显示）
	 * @param bucket 桶名
	 * @param name 上传后显示的文件名称
	 * @return 2小时有效期的路径
	 * @throws Exception
	 */
	public static String fileGeturlGet(String bucket ,String name) {
		String url = null;
		try {
			url = minioClient.getPresignedObjectUrl(
				        GetPresignedObjectUrlArgs.builder()
				            .method(Method.GET)
				            .bucket(bucket)
				            .object(name)
				            .expiry(2, TimeUnit.HOURS) //2小时有效
				            .build());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return url ;
	}
	
	/**
	 * 删除文件
	 * @param bucket 桶名
	 * @param name 上传后显示的文件名称
	 * @throws Exception
	 */
	public static void fileRemove(String bucket ,String name) {
		try {
			minioClient.removeObject( RemoveObjectArgs.builder().bucket(bucket).object(name).build());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}
	
	/**
	 * 下载文件到本地
	 * @param bucket 桶名
	 * @param object 上传后显示的文件名称
	 * @param filename 下载路径
	 */
	public static void fileDownload(String bucket ,String object ,String filename) {
		try {
			minioClient.downloadObject(
					  DownloadObjectArgs.builder()
					  .bucket(bucket)
					  .object(object)
					  .filename(filename)
					  .build());
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ErrorResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InsufficientDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InternalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidResponseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 获取所有桶
	 * @return 桶的集合
	 */
	public static List<Bucket> bucketsGetlist(){
		 List<Bucket> listBuckets = null ;
			 try {
				listBuckets = minioClient.listBuckets();
			} catch (InvalidKeyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ErrorResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InsufficientDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InternalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvalidResponseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return listBuckets;
	}
	
	
	/**
	 * 判断有没有桶，没有创建个
	 * @param bucket 桶名称
	 */
	private static void bucketIf(String bucket) {
		try {// 查询下，看看有没有桶
			boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
			 if (!found) {
	          // 没有创建库
	          minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
			 }
		} catch (Exception e) {
			  System.out.println("Exception: " + e);
		}
	}
	
}
